import type {Router, RouteRecordRaw} from 'vue-router';

import {usePermissionStoreWithOut} from '/@/store/modules/permission';

import {PageEnum} from '/@/enums/pageEnum';
import {useUserStoreWithOut} from '/@/store/modules/user';

import {PAGE_NOT_FOUND_ROUTE} from '/@/router/routes/basic';

import {RootRoute} from '/@/router/routes';

import {isOAuth2AppEnv} from '/@/views/sys/login/useLogin';

const LOGIN_PATH = PageEnum.BASE_LOGIN;
//第三方回调登录路由
const THIRD_LOGIN_PAGE_PATH = PageEnum.THIRD_LOGIN_PAGE_PATH;

const ROOT_PATH = RootRoute.path;

//vue3不支持auth2登录
const whitePathList: PageEnum[] = [LOGIN_PATH, THIRD_LOGIN_PAGE_PATH];

export async function createPermissionGuard(router: Router) {
  const userStore = useUserStoreWithOut();
  const permissionStore = usePermissionStoreWithOut();
  router.beforeEach(async (to, from, next) => {
    console.log("from:", from.path, "to: ", to.path);

    if (
      from.path === ROOT_PATH &&
      to.path === PageEnum.BASE_HOME &&
      userStore.getUserInfo.homePath &&
      userStore.getUserInfo.homePath !== PageEnum.BASE_HOME
    ) {
      const homePath = userStore.getUserInfo.homePath
      next(!homePath || homePath === '/' ? PageEnum.BASE_HOME : homePath);
      return;
    }
    const token = userStore.getToken;

    // Whitelist can be directly entered
    if (whitePathList.filter(e => to.path.startsWith(e))?.length > 0) {
      if (to.path === LOGIN_PATH && token) {
        const isSessionTimeout = userStore.getSessionTimeout;
        try {
          await userStore.afterLoginAction();
          if (!isSessionTimeout) {
            next((to.query?.redirect as string) || PageEnum.BASE_HOME);
            return;
          }
        } catch {}
      } else if (to.path === LOGIN_PATH && isOAuth2AppEnv() && !token) {
        //退出登录进入此逻辑
        //如果进入的页面是login页面并且当前是OAuth2app环境，并且token为空，就进入OAuth2登录页面
        next({ path: THIRD_LOGIN_PAGE_PATH });
        return;
      }
      next();
      return;
    }

    if (to.meta.ignoreAuth) {
      next();
      return;
    }

    // token does not exist
    if (!token) {
      // You can access without permission. You need to set the routing meta.ignoreAuth to true
      if (to.meta.ignoreAuth) {
        next();
        return;
      }

      console.log("token not exist, from:", from, ", to: ", to);

      // vue3 Auth2未实现
      // let path = LOGIN_PATH;
      // if (whitePathList.filter(e => e.startsWith(to.path as PageEnum))?.length > 0) {
      //   // 在免登录白名单，如果进入的页面是login页面并且当前是OAuth2app环境，就进入OAuth2登录页面
      //   if (to.path === LOGIN_PATH && isOAuth2AppEnv()) {
      //     next({ path: THIRD_LOGIN_PAGE_PATH });
      //   } else {
      //     //在免登录白名单，直接进入
      //     next();
      //   }
      // } else {
      //   // 如果当前是在OAuth2APP环境，就跳转到OAuth2登录页面，否则跳转到登录页面
      //   path = isOAuth2AppEnv() ? THIRD_LOGIN_PAGE_PATH : LOGIN_PATH;
      // }
      
      // redirect login page
      const redirectPath = (from.query.redirect || to.query.redirect || to.path) as string;
      const redirect = decodeURIComponent(redirectPath);
      let redirectData: any = { path: redirect, replace: true };
      if (to.path) {
        redirectData = { path: LOGIN_PATH, query: { redirect }, replace: true };
      }
      console.log('redirectData', redirectData);
      next(redirectData)
      return;
    }

    // Jump to the 404 page after processing the login
    if (from.path === LOGIN_PATH && to.name === PAGE_NOT_FOUND_ROUTE.name && to.fullPath !== (userStore.getUserInfo.homePath || PageEnum.BASE_HOME)) {
      next(userStore.getUserInfo.homePath || PageEnum.BASE_HOME);
      return;
    }

    // get userinfo while last fetch time is empty
    if (userStore.getLastUpdateTime === 0) {
      try {
        await userStore.getUserInfoAction();
      } catch (err) {
        console.info(err);
        next();
      }
    }

    if (permissionStore.getIsDynamicAddedRoute) {
      next();
      return;
    }
    const routes = await permissionStore.buildRoutesAction();
    routes.forEach((route) => {
      router.addRoute(route as unknown as RouteRecordRaw);
    });

    router.addRoute(PAGE_NOT_FOUND_ROUTE as unknown as RouteRecordRaw);

    permissionStore.setDynamicAddedRoute(true);

    if (to.name === PAGE_NOT_FOUND_ROUTE.name) {
      // 动态添加路由后，此处应当重定向到fullPath，否则会加载404页面内容
      next({ path: to.fullPath, replace: true, query: to.query });
    } else {
      const redirectPath = (from.query.redirect || to.path) as string;
      const redirect = decodeURIComponent(redirectPath);
      const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect };
      next(nextData);
    }
  });
}
